<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Order extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['h.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['h.city', '=', $city['name']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['time'])) {
                    $create_time = explode(' - ', $filter['time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->order('o.pay_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }
                $_list[$k]['all_address']=$v['province'].$v['city'].$v['area'].$v['address'];
                $_list[$k]['phone12']=$v['people'].'/'.$v['phone'];
            }
            //获取数量
            $total = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $stmt = Db::name('order')->where('id', $data['id'])->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('order')->find($id);
        $this->assign('data', $data);
        return $this->fetch('');

    }

    public function dispatch2(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['dispatch_time']=time();
            $data['row']['dispatch_type']=2;
            $data['row']['status']=7;
//            unset($data['row']['province']);
//            unset($data['row']['city']);
//            unset($data['row']['area']);
            $data['row']['time']=strtotime($data['row']['time']);
            $stmt = Db::name('order')->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('order')->find($id);
        $pz_list=Db::name('pz')->where([
            'status'=>1,
            'is_online'=>1,
        ])->select();
        if ($data['time']){
            $data['time']=date('Y-m-d H:i:s',$data['time']);
        }
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();

        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();

        $city = Db::name('area')->where(['name' => $data['city']])->find();
        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        //医院列表
        $hospital=Db::name('hospital')->where(['is_del'=>0,'area'=>$data['area']])->select();
        //科室列表
        $department_list=Db::name('department')
            ->where(['is_del'=>0,'hospital_id'=>$data['hospita_id'],'pid'=>0])
            ->select();
        //医师
        $doctor_list=Db::name('doctor')->where([
            'is_del'=>0,
            'hospita_id'=>$data['hospital_id']
        ])->select();
        $this->assign([
            'data'=>$data,
            'provice'=>$proviec_list,
            'city_list' => $city_list,
            'area_list' => $area_list,
            'hospital_list'=>$hospital,
            'doctor_list'=>$doctor_list,
            'department_list'=>$department_list,
            'pz_list'=>$pz_list
        ]);
        return $this->fetch('');
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    //退款
    public function refund()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['row']['tui_price']<0){
                $this->error('请输入正确数值！');
            }
            if ($data['row']['tui_price']>$data['row']['pay_money']){
                $this->error('退款金额不能大于支付金额');
            }
            $this->tui($data['row']['id'],$data['row']['tui_price']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('order')->find($id);
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('');
    }

    public function tui($order_id, $tui_money = 0)
    {
        $order_info = Db::name('order')
            ->where('id', $order_id)
            ->find();
        $config = config('wechat.wxpay');
        $app = Factory::payment($config);
        if (!$tui_money) {
            $tui_money = $order_info['pay_money'];
        }
        $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $tui_money * 100, [
            // 可在此处传入其他参数，详细参数见微信支付文档
            'refund_desc' => '订单退款',
        ]);
        tuikuanServerOrder($order_info, $order_info['pay_money'],1);
    }
    public function goods_list(){
        $id = $this->request->param('id/d', 0);
        $goods_list = Db::name('order_goods')
            ->where(['order_id' => $id])
            ->order('sort desc')
            ->select();
        foreach ($goods_list as $k => $v) {
            $goods_info=Db::name('goods')->find($v['goods_id']);
            $goods_list[$k]['goods_info']=$goods_info;
        }
        $this->assign([
            'goods_list' => $goods_list
        ]);
        return $this->fetch('server');
    }

    public function send()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['status']=3;
            $data['row']['express_time']=time();
            $num = Db::name('order')->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        //获取快递公司
        $express = Db::name('express')
            ->where(['is_del' => 0,'status' => 1])
            ->order('sort desc')
            ->select();
        $data = Db::name('order')->find($id);
        $this->assign([
                'data'=>$data,
                'express'=>$express
            ]
        );
        return $this->fetch('');
    }

    public function sendMsg()
    {

        $id = $this->request->param('id/d', 0);
        $express = Db::name('express')
            ->where(['is_del' => 0,'status' => 1])
            ->order('sort desc')
            ->select();
        $data = Db::name('order')->find($id);
        if ($data['express_time']){
            $data['express_time']=date('Y-m-d H:i:s',$data['express_time']);
        }

        $this->assign([
                'data'=>$data,
                'express'=>$express
            ]
        );
        return $this->fetch('');
    }


}